package cn.explink.dop.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.explink.dop.dao.DeliveryCompanyDao;
import cn.explink.dop.dao.DeliveryManDao;
import cn.explink.dop.dao.OrderDao;
import cn.explink.dop.entity.vo.DeliveryCompanyVO;
import cn.explink.dop.entity.vo.OrderVO;
import cn.explink.dop.enums.DeliveryCompanyEnum;
import cn.explink.dop.enums.DeliveryManStatusEnum;
import cn.explink.dop.service.DeliveryCompanyService;
import cn.explink.eap.core.common.service.impl.CommonServiceImpl;

/**
 *
 * @ClassName: DeliveryCompanyServiceImpl
 * @Description: 配送公司管理Service接口实现类
 * @author 孟凡岭
 * @date 2015年5月25日 下午6:12:11
 * @since AR1.0
 */
@Service("deliveryCompanyService")
public class DeliveryCompanyServiceImpl extends CommonServiceImpl implements DeliveryCompanyService {

	@Autowired
	private DeliveryCompanyDao deliveryCompanyDao;
	
	@Autowired
	private DeliveryManDao deliveryManDao; 

	@Autowired
	private OrderDao orderDao;
	/**
	 *
	 * @Title: queryListByPage
	 * @Description: 分页查询
	 * @author 孟凡岭
	 * @param @param company
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @return
	 * @throws
	 */
	@Override
	public Map<String, Object> queryListByPage(DeliveryCompanyVO company, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		return this.deliveryCompanyDao.queryListByPage(company, pageNum, pageSize);
	}

	/**
	 *
	 * @Title: updateEmployeeNum
	 * @Description: 更新配送公司人员数量
	 * @author 孟凡岭
	 * @param @param companyId
	 * @param @param type，类型，1：加，2：减
	 * @param @param num，数量
	 * @throws
	 */
	@Override
	public void updateEmployeeNum(String companyId, int type, int num) {
		// TODO Auto-generated method stub
		this.deliveryCompanyDao.updateEmployeeNum(companyId, type, num);
	}

	/**
	 *
	 * @Title: updateStatus
	 * @Description: 更改配送公司状态(如果状态要改为停用或删除，则把它旗下的员工停用)
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param status
	 * @throws
	 */
	@Override
	public void updateStatus(String[] ids, int status) {
		// TODO Auto-generated method stub
		this.deliveryCompanyDao.updateStatus(ids, status);
		if(status == DeliveryCompanyEnum.Unable.getValue() || status == DeliveryCompanyEnum.DELETE.getValue()){
			for(String id: ids){
				List<String> deliverymanids = this.deliveryManDao.queryDeliveryManByCompanyId(id);
				this.deliveryManDao.updateStatusOnly(deliverymanids, DeliveryManStatusEnum.CANNOT_ACCEPT.getValue());
			}
		}
	}
	
	/**
	 *
	 * @Title: getCanStopCompanyId
	 * @Description: 过滤传入的传入的公司名称，除去不能停用的，得到可以停用的公司名字
	 * @author 孟凡岭
	 * @param @param ids
	 * @param @param status
	 * @throws
	 */
	@Override
	public List<String> getCanStopCompanyId(String[] companyids){
		List<String> canStopIds = new ArrayList<String>();		
		for(String companyid: companyids){
			List<OrderVO> orderVOlist = new ArrayList<OrderVO>();
			List<String> deliveryManIds = this.deliveryManDao.queryDeliveryManByCompanyId(companyid);
			for(String deliveryManId: deliveryManIds){
				orderVOlist.addAll(this.orderDao.getNotFinishOrderbyDeliveryManid(deliveryManId));
			}
			if(orderVOlist.size() == 0){
				canStopIds.add(companyid);
			}
		}
		return canStopIds;
	}
}
